package com.flickr.activities;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import com.flickr.R;
import com.flickr.api.auth.AuthenticationService;
import com.flickr.api.auth.AuthenticationServiceImpl;
import com.flickr.api.auth.Caller;
import com.flickr.api.auth.OAuthHandler;
import com.flickr.callbacks.AuthenticationCallback;
import com.flickr.tasks.AccessTokenTask;
import com.flickr.tasks.AskAuthorizationTask;
import com.flickr.tasks.UserAuthenticationTask;


public class DashboardActivity extends Activity implements AuthenticationCallback, OnClickListener
{
    
    private static final String TAG = DashboardActivity.class.getSimpleName();
    
    private AuthenticationService authenticationService;
    
    private TextView userNameText;

    private Caller mCaller;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dashboard_activity);
        
        userNameText = (TextView) findViewById(R.id.userNameText);
        ImageButton galleryButton = (ImageButton) findViewById(R.id.galleryButton);
        ImageButton albumsButton = (ImageButton) findViewById(R.id.albumsButton);
        ImageButton favoritesButton = (ImageButton) findViewById(R.id.favoritesButton);
        ImageButton contactsButton = (ImageButton) findViewById(R.id.contactsButton);
        
        galleryButton.setOnClickListener(this);
        albumsButton.setOnClickListener(this);
        favoritesButton.setOnClickListener(this);
        contactsButton.setOnClickListener(this);
        
        
        OAuthHandler oauthHandler = OAuthHandler.getHandler();
        
        oauthHandler.load(getPreferences(Context.MODE_PRIVATE));
        authenticationService = new AuthenticationServiceImpl(oauthHandler);
        
        Intent intent = getIntent();
        
        if(isCheckAccessToken(intent)) {
            new AccessTokenTask(intent.getData(), authenticationService, this, this).execute();
            
        } else if (oauthHandler.getAccessToken() == null) {
            new AskAuthorizationTask(this, this).execute();
            
        } else {
            new UserAuthenticationTask(authenticationService, this, this).execute();
        }
    }
    
    private boolean isCheckAccessToken(Intent intent) {
        if(intent == null) {
            return false;
        }
        Uri uri = intent.getData();
        return uri != null &&
                uri.getScheme().equals(OAuthHandler.CALLBACK_URI.getScheme()) &&
                uri.getHost().equals(OAuthHandler.CALLBACK_URI.getHost());
    }

    @Override
    public void onUserLogged(final Caller caller)
    {
        mCaller = caller;
        
        runOnUiThread(new Runnable()
        {
            
            @Override
            public void run()
            {
                userNameText.setText(caller.getRealName());
            }
            
        });
    }

    @Override
    public void onClick(View v)
    {
        if(mCaller == null) {
            return;
        }
        
        Intent intent;
        switch(v.getId()) {
            case R.id.galleryButton:
                intent = new Intent(this, PhotosetGalleryActivity.class);
                break;
            case R.id.albumsButton:
                intent = new Intent(this, PhotosetsActivity.class);
                break;
//            case R.id.favoritesButton:
//                
//                break;
//            case R.id.contactsButton:
//                
//                break;
            default:
                return;
        }
        intent.putExtra("Caller", mCaller);
        startActivity(intent);
    }
    
}
